#! /usr/bin/env python
#coding=utf-8
# -*- coding: utf-8 -*-
import log

#
# transitionMap = {
#    (fromState, event):toState,
#    (fromState, event):toState,
#    ...
# }
#
#
#
class IFsmCallbackObj(object):
	def __init__(self):
		pass

	def OnEnterState(self, state):
		pass

	def OnLeaveState(self, state):
		pass

	def OnEvent(self, state, event):
		pass

class CFsm(object):
	def __init__(self, transitionMap, callbackObj):
		self.transitionMap = transitionMap
		self.callbackObj = callbackObj
		self.curState = None

	def Start(self, startState):
		self.curState = startState
		self.callbackObj.OnEnterState(startState)

	def OnEvent(self, event):
		key = (self.curState, event)
		#print "In FSM.OnEvent:",key
		if self.transitionMap.has_key(key):
			#print "ok!!"
			if self.callbackObj:
				self.callbackObj.OnLeaveState(self.curState)
				self.curState = self.transitionMap[key]
				self.callbackObj.OnEnterState(self.curState)
			else:
				self.curState = self.transitionMap[key]
		else:
			if self.callbackObj:
				self.callbackObj.OnEvent(self.curState, event)
